package com.gopay.common.cps.dao.cbrmb.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.constants.cbrmb.CbRmbIntlBlacklistType;
import com.gopay.common.cps.dao.cbrmb.CbRmbIntlCardBlacklistDAO;
import com.gopay.common.domain.cbrmb.internationalcard.intlcardblacklist.CbRmbIntlCardBlacklist;

/**
 * 外卡黑名单接口DAO实现类
 * @author b.zou 2017-10-26
 */
@Repository("cbRmbIntlCardBlacklistDAO")
public class CbRmbIntlCardBlacklistDAOImpl extends BaseDAOImpl<CbRmbIntlCardBlacklist, Long> implements CbRmbIntlCardBlacklistDAO {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public CbRmbIntlCardBlacklist checkIntlBlackList(CbRmbIntlCardBlacklist entity,CbRmbIntlBlacklistType blacklistType) {
        if(null==blacklistType){
            return null;
        }
        StringBuffer hql = new StringBuffer();
        hql.append(" from CbRmbIntlCardBlacklist where deleteMarker='1' ");
        Map<String,Object> paraMap = new HashMap<>(16);
        String corporationNameValue = entity.getCorporationName();
        String transIpValue = entity.getTransIp();
        String bankcardNoValue = entity.getBankcardNo();
        String cardholderMailValue = entity.getCardholderMail();
        String cardholderFirstNameValue = entity.getCardholderFirstName();
        String cardholderLastNameValue = entity.getCardholderLastName();
        String cardholderPhoneNoValue = entity.getCardholderPhoneNo();
        // 客户名称
        if(CbRmbIntlBlacklistType._01.equals(blacklistType)) {
            if (StringUtils.isNotBlank(corporationNameValue)) {
                paraMap.put("corporationNameValue", corporationNameValue);
                hql.append(" and corporationName=:corporationNameValue ");
            }else{
                return null;
            }
        }
        // 交易ip
        if(CbRmbIntlBlacklistType._05.equals(blacklistType)) {
            if (StringUtils.isNotBlank(transIpValue)) {
                paraMap.put("transIpValue", transIpValue);
                hql.append(" and transIp=:transIpValue ");
            }else{
                return null;
            }
        }
        // 银行卡号
        if(CbRmbIntlBlacklistType._02.equals(blacklistType)) {
            if (StringUtils.isNotBlank(bankcardNoValue)) {
                paraMap.put("bankcardNoValue", SensitiveInfoUtils.getAllHisCipherText(bankcardNoValue).toArray());
                hql.append(" and bankcardNo in (:bankcardNoValue) ");
            }else{
                return null;
            }
        }
        // 邮箱
        if(CbRmbIntlBlacklistType._03.equals(blacklistType)) {
            if (StringUtils.isNotBlank(cardholderMailValue)) {
                paraMap.put("cardholderMailValue", cardholderMailValue);
                hql.append(" and cardholderMail=:cardholderMailValue ");
            }else{
                return null;
            }
        }
        // 名、姓
        if(CbRmbIntlBlacklistType._04.equals(blacklistType)) {
            if (StringUtils.isNotBlank(cardholderFirstNameValue) && StringUtils.isNotBlank(cardholderLastNameValue)) {
                paraMap.put("cardholderFirstNameValue", SensitiveInfoUtils.getAllHisCipherText(cardholderFirstNameValue).toArray());
                paraMap.put("cardholderLastNameValue", SensitiveInfoUtils.getAllHisCipherText(cardholderLastNameValue).toArray());
                hql.append(" and cardholderFirstName in(:cardholderFirstNameValue) ");
                hql.append(" and cardholderLastName in(:cardholderLastNameValue) ");
            }else{
                return null;
            }
        }
        // 手机号
        if(CbRmbIntlBlacklistType._06.equals(blacklistType)) {
            if (StringUtils.isNotBlank(cardholderPhoneNoValue)) {
                paraMap.put("cardholderPhoneNoValue", SensitiveInfoUtils.getAllHisCipherText(cardholderPhoneNoValue).toArray());
                hql.append(" and cardholderPhoneNo in(:cardholderPhoneNoValue) ");
            }else{
                return null;
            }
        }
        logger.info("sql:{};params:{}",new Object[]{hql.toString(),paraMap});
        List<CbRmbIntlCardBlacklist> list = findByHql(hql.toString(), paraMap);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
